package com.my.goodsmanage.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.my.goodsmanage.dao.PhoneMapper;
import com.my.goodsmanage.dao.UserMapper;
import com.my.goodsmanage.entity.Phone;
import com.my.goodsmanage.entity.PhoneResult;
import com.my.goodsmanage.entity.User;
import com.my.goodsmanage.exception.PhoneException;
import com.my.goodsmanage.service.PhoneService;
import com.my.goodsmanage.service.UserService;
import com.my.goodsmanage.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 徐佳辉
 * @date 2020/8/3 16:22
 */
@Service
@Slf4j
public class PhoneServiceImpl extends ServiceImpl<PhoneMapper, Phone> implements PhoneService {


    /**
     * @param phoneNum
     * @return
     */
    @Override
    public int send(String phoneNum) throws PhoneException {




        int i = RandomUtil.randomInt(1000, 9999);
        String host = "https://cxwg.market.alicloudapi.com";
        String path = "/sendSms";
        String method = "POST";
        String appcode = "42f23eaf049f406ea4d43eb85fb041e2";
        //请自己购买网址https://market.aliyun.com/products/57126001/cmapi00039785.html?spm=5176.2020520132.101.1.6e7c7218aeYpb2#sku=yuncode3378500001
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("content", "【物品管理】你的验证码是：" + i + "，3分钟内有效！");
        querys.put("mobile", phoneNum);
        Map<String, String> bodys = new HashMap<String, String>();


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */

            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            log.info("[{}][{}][{}]", response.getLocale(), response.getParams(), response.getAllHeaders());
            System.out.println(response.toString());
            //获取response的body
            int statusCode = response.getStatusLine().getStatusCode();
            log.info(String.valueOf(statusCode));
            if (statusCode >= 400) {
                return 555;
            }
            PhoneResult parse = JSONObject.parseObject(EntityUtils.toString(response.getEntity()),PhoneResult.class);
            log.info("实体类：[{}]",parse);
//            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            throw new PhoneException("短信api出错");
        }

        return i;
    }
}
